// 引入依赖的配置文件模块
require(["config"], function(){
	// 引入依赖模块
	require(["jquery", "template","header","carousel"], function($,template){
		function Nav(){			
			this.loadCarousel();
			this.renderList();
			this.floorNav();
		}
		
		Nav.prototype = {
			constructor:Nav,
			//楼层导航
			floorNav:function(){
				var offon = true;
				//楼层滚动事件
				$(window).scroll(function(){
					if(offon){
				        //获取滚动高度
				        var _top = $(window).scrollTop();
				        if(_top>550){
				            $('.right_nav').show();
				            $(".topsearch-wrap").show()
				        }else{
				            $('.right_nav').hide();
				            $(".topsearch-wrap").hide();
				        }
				        $('.container .floor').each(function(i){
				            //获取当前下标
				            var _index = $(this).index();
				            var _height = $(this).offset().top+428;//获取上偏移值
				            if(_height > _top){//优先原则
				                $('.right_nav ul li').eq(_index).addClass('on').siblings().removeClass('on');
				                return false;//跳出遍历
				            }
				        });
			        }
   				});
   				//侧栏按钮点击事件
			    $('.right_nav ul li').click(function(){
			    	console.log(this);
			        offon = false;
			        var _index = $(this).index();
			        $(this).addClass('on').siblings().removeClass('on');
			        var _height = $('.container .floor').eq(_index).offset().top-100;//获取上偏移值
			        $('body,html').animate({scrollTop:_height},500,function(){
			            offon = true;
			        });
				});
			},
//			banner图轮播
			loadCarousel: function() {
				$($("banner .wrap .banner_box")).carousel({
					imgs : [
						{href:"#", src:"/images/banner1.jpg"},
						{href:"#", src:"/images/banner2.jpg"},
						{href:"#", src:"/images/banner3.jpg"}					
					],
					width: 770,
					height: 420,
					duration: 3000
				});
			},
			
			//渲染商品列表数据
			renderList: function(){
				$.ajax("http://rap2api.taobao.org/app/mock/87071/api/list")
				.done(function(data){
					var data = {list: data.res_body.list};
//					console.log(data);
					var html = template("list-template",data);
					console.log(html);
					$(".prod-context").html(html);
				});
			}
			
		}
		new Nav();
	});
});